//
//  WoDeYeViewController.m
//  PlayBall
//
//  Created by yanfa on 16/4/26.
//
//

#import "WoDeYeViewController.h"

@interface WoDeYeViewController ()

{
    int tabIndex;
    int selectLotIndex;
}

@end

@implementation WoDeYeViewController

SHARED_VIEW_CONTROLLER_IMPLEMENT(WoDeYeViewController)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:@"我的佣金"];
    customLab.textAlignment = NSTextAlignmentCenter;
    
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    //self.navigationItem.hidesBackButton = YES;
    
    _btn_selectLot.layer.borderWidth = 1;
    _btn_selectLot.layer.borderColor = [UIColor darkGrayColor].CGColor;
    _btn_selectLot.layer.cornerRadius = 3;
    _btn_selectLot.clipsToBounds = YES;
    
    _btn_selectMonth.layer.borderWidth = 1;
    _btn_selectMonth.layer.borderColor = [UIColor darkGrayColor].CGColor;
    _btn_selectMonth.layer.cornerRadius = 2;
    _btn_selectMonth.clipsToBounds = YES;
    
    _btn_selectYear.layer.borderWidth = 1;
    _btn_selectYear.layer.borderColor = [UIColor darkGrayColor].CGColor;
    _btn_selectYear.layer.cornerRadius = 2;
    _btn_selectYear.clipsToBounds = YES;
    
    _tf_userName.layer.borderWidth = 1;
    _tf_userName.layer.borderColor = [UIColor darkGrayColor].CGColor;
    _tf_userName.layer.cornerRadius = 3;
    _tf_userName.clipsToBounds = YES;
    
    selectMonthIndex = 0;
    selectYear = @"2016";
    selectYearIndex = 2;
    tabIndex = 0;
    selectLotIndex = 0;
    [_segTab setSelectedSegmentIndex:tabIndex];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getDatebyTabIndex:tabIndex];
}

-(void)getDatebyTabIndex:(int) tabIndex{
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *  currDate=[NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [_label_no_data_tips setHidden:YES];
    if(tabIndex == 0){
        [_la_lot setHidden:YES];
        [_tf_userName setHidden:YES];
        [_la_userName setHidden: YES];
        [_btn_selectLot setHidden:YES];
        [_la_selectYear setHidden:NO];
        [_la_selectMonth setHidden:NO];
        [_btn_selectMonth setHidden:NO];
        [_btn_selectYear setHidden:NO];
        [_btn_search setHidden:NO];
        [_btn_end_date setHidden:YES];
        [_btn_start_date setHidden:YES];
        [_la_jiaoyidateend setHidden:YES];
        [_la_jiaoyidate setHidden:YES];
        
        [_view_myYongjin setHidden:NO];
        [_view_yongjinbili setHidden:YES];
        [_view_detail setHidden:YES];
        _la_end_des.text = @"";
    }else if(tabIndex == 1){
        [_la_lot setHidden:YES];
        [_tf_userName setHidden:YES];
        [_la_userName setHidden: YES];
        [_btn_selectLot setHidden:YES];
        [_la_selectYear setHidden:YES];
        [_la_selectMonth setHidden:YES];
        [_btn_selectMonth setHidden:YES];
        [_btn_selectYear setHidden:YES];
        [_btn_search setHidden:YES];
        [_btn_end_date setHidden:YES];
        [_btn_start_date setHidden:YES];
        [_la_jiaoyidateend setHidden:YES];
        [_la_jiaoyidate setHidden:YES];
        
        [_view_myYongjin setHidden:YES];
        [_view_yongjinbili setHidden:NO];
        [_view_detail setHidden:YES];
        _la_end_des.text = @"";
    }else if(tabIndex == 2){
        [_la_lot setHidden:NO];
        [_tf_userName setHidden:NO];
        [_la_userName setHidden: NO];
        [_btn_selectLot setHidden:NO];
        [_la_selectYear setHidden:YES];
        [_la_selectMonth setHidden:YES];
        [_btn_selectMonth setHidden:YES];
        [_btn_selectYear setHidden:YES];
        
        [_btn_end_date setHidden:NO];
        [_btn_start_date setHidden:NO];
        [_la_jiaoyidateend setHidden:NO];
        [_la_jiaoyidate setHidden:NO];
        [_btn_search setHidden:NO];
        
        [_view_myYongjin setHidden:YES];
        [_view_yongjinbili setHidden:YES];
        [_view_detail setHidden:NO];
        _la_end_des.text = @"";
        selectLotIndex = 0;
        selectLotId = @"0";
        _la_end_des.text = @"";
        NSDateComponents *adcomps = [[NSDateComponents alloc] init];
        [adcomps setYear:0];
        [adcomps setMonth:0];
        [adcomps setDay:-6];
        NSDate *startdate = [calendar dateByAddingComponents:adcomps toDate:currDate options:0];
        currSearchDate=[dateformatter stringFromDate:currDate];
        startDateStr = [dateformatter stringFromDate:startdate];
        [_btn_end_date setTitle:currSearchDate forState:UIControlStateNormal];
        [_btn_start_date setTitle:startDateStr forState:UIControlStateNormal];
        [_btn_end_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_btn_start_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [_btn_selectLot setTitle:@"全部" forState:UIControlStateNormal];
        [_btn_selectLot setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        
    }
    [self requestData];
}

-(void)requestData{
    [dataUnions removeAllObjects];
    [_tableIist reloadData];
    if(tabIndex == 0){
        GetMyYongjin *operation = [[GetMyYongjin alloc] init];
        operation.month= [NSString stringWithFormat:@"%d",selectMonthIndex];
        operation.year = selectYear;
        operation.delegate = self;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }else if(tabIndex == 1){
        GetYJBL *operation = [[GetYJBL alloc] init];
//        operation.userName = _tf_username.text;//[JddUserProfile sharedInstance].name;
//        operation.startDate= startDateStr;
//        operation.endDate = currSearchDate;
        operation.delegate = self;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }else if(tabIndex == 2){
        MyYJDetail *operation = [[MyYJDetail alloc] init];
        operation.userName = _tf_userName.text;//[JddUserProfile sharedInstance].name;
        operation.startDate= startDateStr;
        operation.endDate = currSearchDate;
        operation.delegate = self;
        operation.lotteryid = selectLotId;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }
}

-(void)GetMyYongjinResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        SBJsonParser *jsonParse = [[SBJsonParser alloc] init];
        NSString *realresult = [JddDataParser parseBuyHistoryDetailResult:result];
        NSDictionary *dataDic = [jsonParse objectWithString:realresult];
        dataUnions = [dataDic objectForKey:@"Union"];
        _la_end_des.text = [NSString stringWithFormat:@"汇总   销量:%.2f  佣金:%.2f",[[dataDic objectForKey:@"Sales"] floatValue],[[dataDic objectForKey:@"Bouns"] floatValue]];
        if ([dataUnions count] < 1) {
            [_label_no_data_tips setHidden:NO];
        }else{
            [_label_no_data_tips setHidden:YES];
        }
        [_tableIist reloadData];
    }
}

-(void)GetYJBLResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        SBJsonParser *jsonParse = [[SBJsonParser alloc] init];
        NSString *realresult = [JddDataParser parseBuyHistoryDetailResult:result];
//        NSDictionary *dataDic = [jsonParse objectWithString:realresult];
//        dataUnions = [dataDic objectForKey:@"Union"];
        dataUnions = [jsonParse objectWithString:realresult];
//        _la_end_des.text = [NSString stringWithFormat:@"汇总   销量:%.2f  佣金:%.2f",[[dataDic objectForKey:@"Sales"] floatValue],[[dataDic objectForKey:@"Bouns"] floatValue]];
        //        if ([dataUnions count] < 1) {
        //            [_label_no_data_tips setHidden:NO];
        //        }
        [_tableIist reloadData];
    }
}
-(void)GetMyYJDetailResult:(NSData *)result{
     [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        SBJsonParser *jsonParse = [[SBJsonParser alloc] init];
        NSString *realresult = [JddDataParser parseBuyHistoryDetailResult:result];
        NSDictionary *dataDic = [jsonParse objectWithString:realresult];
        dataUnions = [dataDic objectForKey:@"Union"];
        _la_end_des.text = [NSString stringWithFormat:@"销量:%.2f彩豆  佣金:%.2f彩豆  共%@条",[[dataDic objectForKey:@"Money"] floatValue],[[dataDic objectForKey:@"Bouns"] floatValue],[dataDic objectForKey:@"Total"]];
        if ([dataUnions count] < 1) {
            [_label_no_data_tips setHidden:NO];
        }else{
            [_label_no_data_tips setHidden:YES];
        }
        [_tableIist reloadData];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [_tf_userName resignFirstResponder];
    return YES;
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_tf_userName resignFirstResponder];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_tf_userName resignFirstResponder];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tabIndex == 0){
        MyYongjin3TableViewCell*cell = [MyYongjin3TableViewCell getInstanceView];
        NSDictionary* item = [dataUnions objectAtIndex:indexPath.row];
        cell.la_1.text= [NSString stringWithFormat:@"%@年%@月",selectYear,[item objectForKey:@"Month"]];
        cell.la_2.text= [NSString stringWithFormat:@"%.2f",[[item objectForKey:@"Sales"] floatValue]];
        cell.la_3.text= [NSString stringWithFormat:@"%.2f",[[item objectForKey:@"Bonus"] floatValue]];
        return cell;
    }else if (tabIndex == 1){
        MyYong2TableViewCell*cell = [MyYong2TableViewCell getInstanceView];
        NSDictionary* item = [dataUnions objectAtIndex:indexPath.row];
        cell.la_1.text= [item objectForKey:@"LottName"];
        cell.la_2.text = [NSString stringWithFormat:@"%@%%",[item objectForKey:@"Scale"]];
        return cell;
    }else if (tabIndex == 2){
//        YJDET_TableViewCell *cell = [YJDET_TableViewCell getInstanceView];
        MYYjDetailTableViewCell *cell = [MYYjDetailTableViewCell getInstanceView];//[[MYYjDetailTableViewCell alloc] init];
        NSDictionary* item = [dataUnions objectAtIndex:indexPath.row];
        cell.la_lotName.text = [item objectForKey:@"LottName"];
        cell.la_money.text = [item objectForKey:@"Money"];
//        cell.la_time.text = [item objectForKey:@"DateTime"];
        cell.la_time.text = [[item objectForKey:@"DateTime"] stringByReplacingOccurrencesOfString:@" " withString:@"\n"];
        cell.la_yj.text = [item objectForKey:@"Bouns"];
        cell.la_userName.text = [item objectForKey:@"UserName"];
        return cell;
    }else{
        return nil;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataUnions count];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tabIndex == 0){
//        NSLog(@"%d",indexPath.row);
//        //        SetYongJinViewController *controller = [SetYongJinViewController sharedInstance];
//        SetYongJinViewController *controller = [[SetYongJinViewController alloc] init];
//        NSDictionary* item = [dataUnions objectAtIndex:indexPath.row];
//        controller.subuserid = [item objectForKey:@"UserID"];
//        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30.0f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    
    if([actionSheet isMemberOfClass:[MyGetDateView class]]){
        MyGetDateView *locateViewTemp = (MyGetDateView *)actionSheet;
        if(buttonIndex == 0) {
        }else {
            if(locateViewTemp.callByWhoId == 0){
                [_btn_start_date setTitle:locateViewTemp.selectDate forState:UIControlStateNormal];
                [_btn_start_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                startDateStr = locateViewTemp.selectDate;
            }else{
                [_btn_end_date setTitle:locateViewTemp.selectDate forState:UIControlStateNormal];
                [_btn_end_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                currSearchDate = locateViewTemp.selectDate;
            }
        }
    }else if([actionSheet isMemberOfClass:[PCLocationView class]]){
        PCLocationView *locateViewTemp = (PCLocationView *)actionSheet;
        if(buttonIndex == 0) {
        }else {
            if(locateViewTemp.type == 0){
                [_btn_selectLot setTitle:locateViewTemp.selectLotName forState:UIControlStateNormal];
                [_btn_selectLot setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                selectLotIndex = locateViewTemp.selectIndex;
                selectLotId = locateViewTemp.selectLotId;
            }else if (locateViewTemp.type == 1) {
                [_btn_selectYear setTitle:locateViewTemp.selectStr forState:UIControlStateNormal];
                [_btn_selectYear setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                selectYear = locateViewTemp.selectStr;
                selectYearIndex = locateViewTemp.selectIndex;
            }else if (locateViewTemp.type == 2){
                [_btn_selectMonth setTitle:locateViewTemp.selectStr forState:UIControlStateNormal];
                [_btn_selectMonth setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                selectMonth = locateViewTemp.selectStr;
                selectMonthIndex = locateViewTemp.selectIndex;
            }
        }
    }
}

- (IBAction)btn_click_lotSelect:(id)sender {
    getLotView = [[PCLocationView alloc] initWithTitle:@"选择彩种" delegate:self];
    [getLotView showInView:self.view withSelectIndex:selectLotIndex];
}
- (IBAction)btn_click_selectMonth:(id)sender {
    getLotView = [[PCLocationView alloc] initWithTitle:@"选择月份" delegate:self];
    [getLotView showInView:self.view withType:2 withSelectIndex:selectMonthIndex];
}

- (IBAction)btn_click_selectYear:(id)sender {
    getLotView = [[PCLocationView alloc] initWithTitle:@"选择年份" delegate:self];
    [getLotView showInView:self.view withType:1 withSelectIndex:selectYearIndex];
}

- (IBAction)btn_click_search:(id)sender {
    [_tf_userName resignFirstResponder];
    [self requestData];
}
- (IBAction)tabSegSelectChanged:(id)sender {
    tabIndex = _segTab.selectedSegmentIndex;
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    [self getDatebyTabIndex:tabIndex];
}
- (IBAction)btnStratDateClick:(id)sender {
    getDateView = [[MyGetDateView alloc] initWithTitle:@"选择日期" delegate:self];
    getDateView.callByWhoId = 0;
    [getDateView showInView:self.view];
}

- (IBAction)btnEndDateClick:(id)sender {
    getDateView = [[MyGetDateView alloc] initWithTitle:@"选择日期" delegate:self];
    getDateView.callByWhoId = 1;
    [getDateView showInView:self.view];
}
@end
